var express = require('express');
var router = express.Router();
const request = require('superagent');

router.get('/proxy', function(req, res, next){
  const query = req.query;
  if(!query){
    return next('没有query');
  }
  request(`http://maps.google.com/maps/api/geocode/json?address=${encodeURIComponent(query)}`)
  .then(d => d.text)
  .then(d => {
    res.send(d);
  })
  .catch(e => {
    next(e);
  });
});

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});


module.exports = router;
